package com.oocl.crmweb.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.oocl.crmweb.pojo.Customer;
import com.oocl.crmweb.service.CustomerManager;

public class CustomerManagerImplMock implements CustomerManager {
	
	private List<Customer> list = new ArrayList<>();

	CustomerManagerImplMock() {
		for (int i = 0; i < 5; i++) {
			Customer c = new Customer(i, "Mike", Math.random() * 5 + 10, Math.random() < 0.5, new Date(), new String[] { "Maven", "Eclipse" });
			list.add(c);
		}
	}
	
	@Override
	public int addCustomer(Customer c) {
		c.setId(list.size() + (int) (Math.random() * 5));
		list.add(c);
		return 1;
	}

	@Override
	public List<Customer> findAllCustomers() {
		return list;
	}
	
	@Override
	public Customer loadCustomer(Integer id) {
		Customer result = null;
		for (Customer c : list) {
			if (id.equals(c.getId())) {
				result = c;
				break;
			}
		}
		return result;
	}

	@Override
	public int updateCustomer(Customer c) {
		int i = 0;
		for (; i < list.size(); i++) {
			if (list.get(i).getId().equals(c.getId())) {
				list.set(i, c);
				return 1;
			}
		}
		return 0;
	}

	@Override
	public int deleteCustomer(Integer id) {
		int i = 0;
		for (; i < list.size(); i++) {
			if (list.get(i).getId().equals(id)) {
				list.remove(i);
				return 1;
			}
		}
		return 0;
	}

}
